package com.ff.xyh.service.post.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.common.entity.post.PostJoiner;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.qo.post.PostJoinerQuery;
import com.ff.xyh.service.post.service.PostJoinerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 参与用户表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@RestController
@RequestMapping("/post-service")
public class PostJoinerController {
    @Autowired
    private PostJoinerService postJoinerService;


    /**
     * 拼单参与者 - 条件查询，包含分页
     * @param postJoinerQuery
     * @return
     */
    @PostMapping("get-complaint-handle-condition")
    public ResultObject getPostJoinerCondition(
            @RequestBody PostJoinerQuery postJoinerQuery) {
        return postJoinerService.getPostJoinerCondition(postJoinerQuery);
    }

    @PostMapping("get-joiner-post-id-type")
    public ResultObject getEffectivePostJoinersWithPostIdAndPostType(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") int postType) {
        QueryWrapper<PostJoiner> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId)
                .eq("post_type", postType)
                .le("joiner_status",0) // ≤0
                .eq("is_deleted", 0);
        List<PostJoiner> list = postJoinerService.list(wrapper);
        return ResultObject.ok().data("items", list);
    }

    @PostMapping("get-accepted-joiner-post-id-type")
    public ResultObject getAcceptedPostJoinersWithPostIdAndPostType(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") int postType) {
        QueryWrapper<PostJoiner> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId)
                .eq("post_type", postType)
                .eq("joiner_status",-1) // -1
                .eq("is_deleted", 0);
        List<PostJoiner> list = postJoinerService.list(wrapper);
        return ResultObject.ok().data("items", list);
    }

    /**
     * 根据帖子和用户id，查询拼单参与者
     * @param postId
     * @param postType
     * @param userId
     * @param talkRequired 是否筛选可发言的
     * @return
     */
    @PostMapping("get-joiner-post-id-type-user-id-strict")
    public ResultObject getPostJoinerWithPostIdAndTypeAndUserId(
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") int postType,
            @RequestParam(name = "userId") String userId,
            @RequestParam(name = "strict", required = false) boolean talkRequired) {
        QueryWrapper<PostJoiner> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId)
                .eq("post_type", postType)
                .eq("user_id", userId)
                .eq("is_deleted", 0);
        // 需要获取已接受的
        if (talkRequired) {
            wrapper.eq("joiner_status", -1); // = -1
        } else {
            wrapper.eq("joiner_status", -1)
                    .or()
                    .eq("joiner_status", 0); // = 0
        }
        PostJoiner postJoiner = postJoinerService.getOne(wrapper);
        return ResultObject.ok().data("data", postJoiner);
    }

    @PostMapping("get-joiners-user-id-status")
    public ResultObject getPostJoinerListByUserIdAndJoinerStatus(
            @RequestParam(name = "joinerStatus") int joinerStatus,
            @RequestParam(name = "userId") String userId) {
        QueryWrapper<PostJoiner> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("is_deleted", 0)
                .eq("joiner_status", joinerStatus);
        List<PostJoiner> joinerList = postJoinerService.list(wrapper);
        if (joinerList.isEmpty())
            return ResultObject.error().message(ResultMessage.NO_RESULT);

        return ResultObject.ok().data("items", joinerList);
    }

    @PostMapping("insert-joiner")
    public ResultObject insertPostJoiner(
            @RequestBody PostJoiner postJoiner) {
        QueryWrapper<PostJoiner> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postJoiner.getPostId())
                .eq("post_type", postJoiner.getPostType())
                .eq("user_id", postJoiner.getUserId());
        if (postJoinerService.getOne(wrapper) != null) {
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (postJoinerService.save(postJoiner)) {
                return ResultObject.ok().data("data", postJoiner);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }

    @PostMapping("delete-joiner")
    public ResultObject deletePostJoiner(
            @RequestBody PostJoiner postJoiner) {
        QueryWrapper<PostJoiner> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postJoiner.getPostId())
                .eq("post_type", postJoiner.getPostType())
                .eq("user_id", postJoiner.getUserId());
        if (postJoinerService.getOne(wrapper) == null) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (postJoinerService.remove(wrapper)) {
                return ResultObject.ok().data("data", postJoiner);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    /**
     * 修改参与数量
     * @param postId
     * @param postType
     * @param number
     * @return
     */
    @PostMapping("change-joiner-number")
    public ResultObject changePostJoinerNumber(
            @RequestParam("postId") String postId,
            @RequestParam("postType") Integer postType,
            @RequestParam("userId") String userId,
            @RequestParam("number") Integer number) {
        QueryWrapper<PostJoiner> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId)
                .eq("post_type", postType)
                .eq("user_id", userId);
        PostJoiner existJoiner = postJoinerService.getOne(wrapper);
        if (existJoiner == null) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            existJoiner.setNumber(number).setGmtModified(new Date());
            if (postJoinerService.update(existJoiner, wrapper)) {
                return ResultObject.ok().data("data", existJoiner);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }

    @PostMapping("change-joiner-status")
    public ResultObject changePostJoinerStatus(
            @RequestParam("postId") String postId,
            @RequestParam("postType") Integer postType,
            @RequestParam("userId") String userId,
            @RequestParam("joinerStatus") Integer joinerStatus) {
        QueryWrapper<PostJoiner> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId)
                .eq("post_type", postType)
                .eq("user_id", userId);
        PostJoiner existJoiner = postJoinerService.getOne(wrapper);
        if (existJoiner == null) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            existJoiner.setJoinerStatus(joinerStatus).setGmtModified(new Date());
            if (postJoinerService.update(existJoiner, wrapper)) {
                return ResultObject.ok().data("data", existJoiner);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }


}

